package org.uim.cricket.entities.persist.impl;

import org.uim.cricket.entities.Player;
import org.uim.cricket.entities.batting.BatsmanOutDetails;
import org.uim.cricket.entities.persist.CricketEntityPersistable;

public class XMLBatsmanOutDetailPersist implements
		CricketEntityPersistable<BatsmanOutDetails> {

	private String tagName = "OutDetails";

	@Override
	public String doPersist(BatsmanOutDetails object) {
		if (object == null) return "";
		StringBuilder sb = new StringBuilder();

		if (tagName.length() > 0) sb.append("<").append(tagName);

		if (object.getOutType() != null) {
			sb.append(" outType=\"").append(object.getOutType()).append("\"");
		}
		if (object.getOutAsWicket() >= 0) {
			sb.append(" outWicket=\"").append(object.getOutAsWicket()).append("\"");
		}
		if (object.getBowler() != null) {
			sb.append(" bowler=\"").append(object.getBowler().getId()).append("\"");
		}
		if (object.getFielders() != null) {
			Player[] fielders = object.getFielders();
			boolean[] wks = object.getWicketKeeperFielders();
			String fields = "";
			for (int j = 0; j < fielders.length; j++) {
				if (j > 0) fields += ",";
				if (wks != null && wks[j]) fields += "+";
				fields += fielders[j].getId();
			}
			sb.append(" fielders=\"").append(fields).append("\"");
		}
		
		if (tagName.length() > 0) sb.append(" />");
		return sb.toString();
	}

	public void setTagName(String tagName) {
		this.tagName = tagName;
	}

	public String getTagName() {
		return tagName;
	}

}
